package vn.orderonline.web.admin.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import vn.orderonline.com.core.business.drink.IDrink;
import vn.orderonline.com.core.business.dto.DrinkDto;
import vn.orderonline.com.core.business.menudrink.IMenuDrink;
import vn.orderonline.com.core.business.restaurant.IRestaurantManager;

@Controller
@RequestMapping(value = "/drink")
public class DrinkController {

	private static final Logger LOGGER = Logger
			.getLogger(DrinkController.class);

	@Autowired
	private IDrink iDrink;
	@Autowired
	private IMenuDrink iMenuDrink;
	@Autowired
	private IRestaurantManager iRestaurantManager;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getListDrink(@RequestParam Integer restaurantId,
			@RequestParam Integer menuDrinkId, ModelMap model, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START GET LIST ALL DRINK!");
			DrinkDto drink = null;
			try {
				drink = new DrinkDto();
				drink.setRestaurantId(restaurantId);
				drink.setRestaurantName(iRestaurantManager.getRestaurantNameByRestaurantId(restaurantId));
				drink.setMenuDrinkId(menuDrinkId);
				drink.setMenuDrinkName(iMenuDrink.getMenuDrinkNameByMenuDrinkId(menuDrinkId));
				model.addAttribute("drink", drink);
				model.addAttribute("list", iDrink.getAllDrinkByResIdAndMenuDrinkId(
						restaurantId, menuDrinkId));
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN GET LIST ALL DRINK!", e);
			}
			LOGGER.info("END GET LIST ALL DRINK!");
			return "/drink/searchdrink";
		} else {
			return "redirect:/adminaccount/login";
		}
	}

	@RequestMapping(value = "/addform", method = RequestMethod.GET)
	public String createFormAdd(@RequestParam Integer restaurantId,
			@RequestParam Integer menuDrinkId, ModelMap model, HttpServletRequest request) {
			LOGGER.info("START CREATE FORM ADD NEW DRINK!");
		if (request.getUserPrincipal() != null) {
			DrinkDto drink = null;
			try {
				drink = new DrinkDto();
				drink.setRestaurantId(restaurantId);
				drink.setRestaurantName(iRestaurantManager.getRestaurantNameByRestaurantId(restaurantId));
				drink.setMenuDrinkId(menuDrinkId);
				drink.setMenuDrinkName(iMenuDrink.getMenuDrinkNameByMenuDrinkId(menuDrinkId));
				model.addAttribute("drink", drink);
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN CREATE FORM ADD NEW DRINK!", e);
			}
			LOGGER.info("END CREATE FORM ADD NEW DRINK!");
			return "/drink/adddrink";
		} else {
			return "redirect:/adminaccount/login";
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	public String addDrink(@ModelAttribute DrinkDto drink,
			RedirectAttributes redirectAttris, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START ADD NEW DRINK!");
			try {
				drink.setCreateId(request.getUserPrincipal().getName());
				iDrink.addDrink(drink);
				redirectAttris
						.addAttribute("restaurantId", drink.getRestaurantId());
				redirectAttris.addAttribute("menuDrinkId", drink.getMenuDrinkId());
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN ADD NEW DRINK!", e);
			}
			LOGGER.info("END WHEN ADD NEW DRINK!");
			return "redirect:/drink/list";
		} else {
			return "redirect:/adminaccount/login";
		}
	}

	@RequestMapping(value = "/checkexisteddrink", method = RequestMethod.POST)
	@ResponseBody
	public String checkExistedDrink(@RequestParam Integer menuDrinkId,
			@RequestParam String drinkName) {
		LOGGER.info("START CHECK EXISTED DRINK NAME!");
		Boolean flag = false;
		try {
			flag = iDrink.checkExistedDrink(menuDrinkId, drinkName);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CHECK EXISTED DRINK NAME!", e);
		}
		LOGGER.info("END CHECK EXISTED DRINK NAME!" + flag.toString());
		return flag.toString();
	}

	@RequestMapping(value = "/viewDrink", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
	public String viewDetailDrink(@RequestParam Integer drinkId, ModelMap model, HttpServletRequest request) {
		LOGGER.info("START VIEW DETAIL DRINK!");
		if (request.getUserPrincipal() != null) {
			DrinkDto drink = null;
			try {
				drink = iDrink.viewDetail(drinkId);
				model.addAttribute("drink", drink);
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN VIEW DETAIL DRINK!", e);
			}
			LOGGER.info("END VIEW DETAIL DRINK!");
			return "/drink/detaildrink";
		} else {
			return "redirect:/adminaccount/login";
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
	public String deleteDrink(@RequestParam Integer restaurantId,
			@RequestParam Integer menuDrinkId, @RequestParam Integer drinkId,
			RedirectAttributes redirectAttris, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START DELETE DRINK!");
			try {
				iDrink.deleteDrink(menuDrinkId, drinkId);
				redirectAttris.addAttribute("restaurantId", restaurantId);
				redirectAttris.addAttribute("menuDrinkId", menuDrinkId);
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN DELETE DRINK!", e);
			}
			LOGGER.info("END DELETE DRINK!");
			return "redirect:/drink/list";
		} else {
			return "redirect:/adminaccount/login";
		}
	}
	
	@RequestMapping(value = "/formupdate", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
	public String createFormUpdateDrink(@RequestParam Integer drinkId, ModelMap model, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START CREATE FORM UPDATE DRINK!");
			try {
				model.addAttribute("drink", iDrink.viewDetail(drinkId));
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN CREATE FORM UPDATE DRINK!", e);
			}
			LOGGER.info("END CREATE FORM UPDATE DRINK!");
			return "/drink/updatedrink";
		} else {
			return "redirect:/adminaccount/login";
		}
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	public String updateDrink(@ModelAttribute DrinkDto drink, RedirectAttributes redirectAttris, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START UPDATE DRINK!");
			try {
				drink.setUpdateId(request.getUserPrincipal().getName());
				iDrink.updateDrink(drink);
				redirectAttris.addAttribute("restaurantId", drink.getRestaurantId());
				redirectAttris.addAttribute("menuDrinkId", drink.getMenuDrinkId());
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN UPDATE DRINK!", e);
			}
			LOGGER.info("END UPDATE DRINK!");
			return "redirect:/drink/list";
		} else {
			return "redirect:/adminaccount/login";
		}
	}
	
	@RequestMapping(value = "/searchdrink", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	public String searchDrink(@ModelAttribute DrinkDto drink, ModelMap model, HttpServletRequest request) {
		if (request.getUserPrincipal() != null) {
			LOGGER.info("START SEARCH DRINK!");
			try {
				model.addAttribute("drink", drink);
				model.addAttribute("list", iDrink.searchDrink(drink));
			} catch (Exception e) {
				LOGGER.error("ERROR WHEN SEARCH DRINK!", e);
			}
			LOGGER.info("END SEARCH DRINK!");
		return "/drink/searchdrink";
		} else {
			return "redirect:/adminaccount/login";
		}
	}
}
